package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.mapper.ConsumptionItemMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public Consumption save(String appointmentAno) {
        // 根据预约单流水号查询预约单对象
        Appointment appointment  = appointmentMapper.selectByAno(appointmentAno);
        // 修改预约单的状态为消费中
        appointmentMapper.updateStatus(appointment.getId(), AppointmentStatusEnum.CONSUME.getValue());
        // 创建结算单对象
        Consumption consumption = new Consumption();
        // 设置结算单的状态为待结算状态
        consumption.setStatus(ConsumptionStatusEnum.CONSUME.getValue());
        // 把预约单流水号设置到结算单关联
        consumption.setAppointmentAno(appointment.getAno());
        // 生成结算单的流水号
        String format = DateUtil.format(new Date(), "yyyyMMddHHss");
        String numbers = RandomUtil.randomNumbers(5);
        consumption.setCno(format+numbers);
        // 从预约单中获取客户名称,并设置到结算单中
        consumption.setCustomerName(appointment.getContactName());
        // 从预约单中获取客户联系方式,并设置到结算单中
        consumption.setCustomerTel(appointment.getContactTel());
        // 从预约单中获取预约门店,并设置到结算单中
        consumption.setBusiness(appointment.getBusiness());
        // 进店时间设置为当前时间
        consumption.setCheckinTime(new Date());
        // 创建时间设置为当前时间
        consumption.setCreateTime(new Date());
        // 创建人设置为当前登录用户(员工)
        consumption.setCreateUser(UserContext.getCurrentUser());
        // 把结算单保存到数据库中
        consumptionMapper.insert(consumption);
        return consumption;
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

}
